﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace VodafoneWP7
{
    public partial class WebBrowserPage : PhoneApplicationPage
    {
        public WebBrowserPage()
        {            
            InitializeComponent();
           
        }

        System.Windows.Navigation.LoadCompletedEventHandler nextstep;
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            webBrowser1.IsScriptEnabled = true;

            nextstep = new System.Windows.Navigation.LoadCompletedEventHandler(webBrowser1_LoadCompleted);
            webBrowser1.LoadCompleted += nextstep;
            webBrowser1.Navigate(new Uri(MainPage.NavigationUri));

            
        }

        void webBrowser1_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            webBrowser1.LoadCompleted -= nextstep;

            var co = MainPage.cookieJar.GetCookies(new Uri(MainPage.NavigationUri));            
            foreach (var cook in co)
            {
                var c = cook as Cookie;
                webBrowser1.InvokeScript("eval", new string[] { 
                    "document.cookie = ('" + c + ";" +
                    //"domain=" + c.Domain+ ";"+
                    //"path="+c.Path+";"+
                    "');" });
            }

            string cookieString = webBrowser1.InvokeScript("eval", new string[] { "document.cookie;" }) as string;
            webBrowser1.NavigateToString(MainPage.WebBrowserHTML);

        }
    }
}